package com.genius.mobile_page.pandian_jd.service.impl;

import java.sql.SQLException;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.genius.dingding.util.GeniusConfig;
import com.genius.mobile_page.common.ErrorResponseData;
import com.genius.mobile_page.common.ResponseData;
import com.genius.mobile_page.common.SuccessResponseData;
import com.genius.mobile_page.pandian_jd.service.PandianjdService;
import com.genius.workflow.util.DBTools2;
import com.genius.workflow.util.DBUtils;

import cn.hutool.core.util.ObjectUtil;

@Service("pandianjdService")
public class PandianjdServiceImpl implements PandianjdService{

	@Override
	public ResponseData getShopInfo(JSONObject parms) {
		try {
			StringBuilder sql = new StringBuilder("select sStoreNO shopcode,sStoreDesc shopname from "
					+GeniusConfig.CAIJIDATABASE+".jd_tstore where 1=1");
			
			JSONArray params = new JSONArray();
			if(ObjectUtil.isNotEmpty(parms.getString("shopcode"))){
				sql.append(" and sStoreNO in (");
				String[] shops = parms.getString("shopcode").split(",");
				for (int i = 0; i < shops.length; i++) {
					sql.append("?,");
					params.add(shops[i]);
				}
				sql.deleteCharAt(sql.length()-1);
				sql.append(")");
			}
			JSONArray result = DBUtils.selectArr(sql.toString(),params.toArray());
			if(result != null){
				return new SuccessResponseData(result);
			}else{
				return new ErrorResponseData("没有此门店的信息");
			}
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData getPandianPlan(JSONObject parms) {
		StringBuilder sql = new StringBuilder("select * from pandian_plan_jd where 1=1");
		try {
			JSONArray params = new JSONArray();
			if(ObjectUtil.isNotEmpty(parms.getString("id"))){
				sql.append(" and id = ?");
				params.add(parms.getString("id"));
				JSONObject result = DBUtils.select(sql.toString(),params.toArray());
				if(result != null){
					return new SuccessResponseData(result);
				}else{
					return new ErrorResponseData("没有盘点计划的信息");
				}
			}else{
				if(ObjectUtil.isNotEmpty(parms.getString("startDate"))){
					sql.append(" and pandianDate >= ?");
					params.add(parms.getString("startDate"));
				}
				if(ObjectUtil.isNotEmpty(parms.getString("endDate"))){
					sql.append(" and pandianDate <= ?");
					params.add(parms.getString("endDate"));
				}
				if(ObjectUtil.isNotEmpty(parms.getString("flag"))){
					sql.append(" and flag = ?");
					params.add(parms.getString("flag"));
				}
				if(ObjectUtil.isNotEmpty(parms.getString("shopcode"))){
					sql.append(" and shopcode = ?");
					params.add(parms.getString("shopcode"));
				}
				JSONArray result = DBUtils.selectArr(sql.toString(),params.toArray());
				if(result != null){
					return new SuccessResponseData(result);
				}else{
					return new ErrorResponseData("没有盘点计划的信息");
				}
			}
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData insertPandianPlan(JSONObject parms,HttpSession session) {
		
		try {
			String userId = parms.getString("userId");
			if(ObjectUtil.isEmpty(userId)){
				userId = session.getAttribute("userId")+"";
			}
			DBTools2.insertBatch("pandianDate,shopcode,shopname,flag,lrr='"+userId+"',lrsj=now()"
					+ ",xgr='"+userId+"',xgsj=now()", "pandian_plan_jd", parms.getJSONArray("shopList"));
			return new SuccessResponseData();
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("盘点计划新增失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData editPandianPlan(JSONObject parms, HttpSession session) {
		try {
			String userId = parms.getString("userId");
			if(ObjectUtil.isEmpty(userId)){
				userId = session.getAttribute("userId")+"";
			}
			DBTools2.update("pandianDate,shopcode,shopname,flag,xgr='"+userId+"',xgsj=now()","id", "pandian_plan_jd", parms);
			
			return new SuccessResponseData();
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("盘点计划编辑失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData deletePandianPlan(JSONObject parms) {
		try {
			DBTools2.delete("id", "pandian_plan_jd", parms);
			return new SuccessResponseData();
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("盘点计划删除失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData getPandianBitian(JSONObject parms) {
		StringBuilder sql = new StringBuilder("select * from pandian_item_bipan_jd where 1=1");
		try {
			JSONArray params = new JSONArray();
			if(ObjectUtil.isNotEmpty(parms.getString("id"))){
				sql.append(" and id = ?");
				params.add(parms.getString("id"));
			}else{
				if(ObjectUtil.isNotEmpty(parms.getString("startDate"))){
					sql.append(" and startdate <= ?");
					params.add(parms.getString("startDate"));
				}
				if(ObjectUtil.isNotEmpty(parms.getString("endDate"))){
					sql.append(" and enddate >= ?");
					params.add(parms.getString("endDate"));
				}
				if(ObjectUtil.isNotEmpty(parms.getString("flag"))){
					sql.append(" and flag = ?");
					params.add(parms.getString("flag"));
				}
				if(ObjectUtil.isNotEmpty(parms.getString("name"))){
					sql.append(" and `name` like ?");
					params.add("%"+parms.getString("name")+"%");
				}
			}
			sql.append(" order by sort");
			JSONArray result = DBUtils.selectArr(sql.toString(),params.toArray());
			if(result != null){
				return new SuccessResponseData(result);
			}else{
				return new ErrorResponseData("没有盘点必填项的信息");
			}
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData insertPandianBitian(JSONObject parms, HttpSession session) {
		try {
			String userId = parms.getString("userId");
			if(ObjectUtil.isEmpty(userId)){
				userId = session.getAttribute("userId")+"";
			}
			DBTools2.insertBatch("name,checkpoint,isphoto,photonum,option1,option2,startdate,enddate,flag"
					+ ",sort,lrr='"+userId+"',lrsj=now(),xgr='"+userId+"',xgsj=now()", "pandian_item_bipan_jd", parms.getJSONArray("dataArray"));
			return new SuccessResponseData();
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("盘点必填项新增失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData editPandianBitian(JSONObject parms, HttpSession session) {
		try {
			String userId = parms.getString("userId");
			if(ObjectUtil.isEmpty(userId)){
				userId = session.getAttribute("userId")+"";
			}
			DBTools2.update("name,checkpoint,isphoto,photonum,option1,option2,startdate,enddate,flag"
					+ ",sort,xgr='"+userId+"',xgsj=now()","id", "pandian_item_bipan_jd", parms);
			
			return new SuccessResponseData();
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("盘点必填项编辑失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData deletePandianBitian(JSONObject parms) {
		try {
			DBTools2.delete("id", "pandian_item_bipan_jd", parms);
			return new SuccessResponseData();
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("盘点必填项删除失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData getPandianShopDaiban(JSONObject parms) {
		StringBuilder sql = new StringBuilder("select * from pandian_shop_daiban_jd where 1=1");
		try {
			JSONArray params = new JSONArray();
			if(ObjectUtil.isNotEmpty(parms.getString("id"))){
				sql.append(" and id = ?");
				params.add(parms.getString("id"));
			}else{
				if(ObjectUtil.isNotEmpty(parms.getString("flag"))){
					sql.append(" and flag = ?");
					params.add(parms.getString("flag"));
				}
				if(ObjectUtil.isNotEmpty(parms.getString("itemname"))){
					sql.append(" and itemname like ?");
					params.add("%"+parms.getString("itemname")+"%");
				}
			}
			JSONArray result = DBUtils.selectArr(sql.toString(),params.toArray());
			if(result != null){
				return new SuccessResponseData(result);
			}else{
				return new ErrorResponseData("没有盘点门店待办事项的信息");
			}
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData insertPandianShopDaiban(JSONObject parms, HttpSession session) {
		try {
			String userId = parms.getString("userId");
			if(ObjectUtil.isEmpty(userId)){
				userId = session.getAttribute("userId")+"";
			}
			DBTools2.insertBatch("sort,itemname,flag"
					+ ",lrr='"+userId+"',lrsj=now(),xgr='"+userId+"',xgsj=now()", "pandian_shop_daiban_jd", parms.getJSONArray("dataArray"));
			return new SuccessResponseData();
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("盘点门店待办事项新增失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData editPandianShopDaiban(JSONObject parms, HttpSession session) {
		try {
			String userId = parms.getString("userId");
			if(ObjectUtil.isEmpty(userId)){
				userId = session.getAttribute("userId")+"";
			}
			DBTools2.update("sort,itemname,flag,xgr='"+userId+"',xgsj=now()","id", "pandian_shop_daiban_jd", parms);
			
			return new SuccessResponseData();
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("盘点门店待办事项编辑失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData deletePandianShopDaiban(JSONObject parms) {
		try {
			DBTools2.delete("id", "pandian_shop_daiban_jd", parms);
			return new SuccessResponseData();
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("盘点门店待办事项删除失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}
	
	@Override
	public ResponseData getPandianJieguoBiaozhun(JSONObject parms) {
		StringBuilder sql = new StringBuilder("select * from pandian_jieguo_biaozhun_jd where 1=1");
		try {
			JSONArray params = new JSONArray();
			if(ObjectUtil.isNotEmpty(parms.getString("id"))){
				sql.append(" and id = ?");
				params.add(parms.getString("id"));
			}else{
				if(ObjectUtil.isNotEmpty(parms.getString("flag"))){
					sql.append(" and flag = ?");
					params.add(parms.getString("flag"));
				}
				if(ObjectUtil.isNotEmpty(parms.getString("itemname"))){
					sql.append(" and itemname like ?");
					params.add("%"+parms.getString("itemname")+"%");
				}
			}
			JSONArray result = DBUtils.selectArr(sql.toString(),params.toArray());
			if(result != null){
				return new SuccessResponseData(result);
			}else{
				return new ErrorResponseData("没有盘点结果标准的信息");
			}
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("数据库操作失败！！");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData insertPandianJieguoBiaozhun(JSONObject parms, HttpSession session) {
		try {
			String userId = parms.getString("userId");
			if(ObjectUtil.isEmpty(userId)){
				userId = session.getAttribute("userId")+"";
			}
			DBTools2.insertBatch("sort,itemname,flag"
					+ ",lrr='"+userId+"',lrsj=now(),xgr='"+userId+"',xgsj=now()", "pandian_jieguo_biaozhun_jd", parms.getJSONArray("dataArray"));
			return new SuccessResponseData();
			
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("盘点结果标准新增失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData editPandianJieguoBiaozhun(JSONObject parms, HttpSession session) {
		try {
			String userId = parms.getString("userId");
			if(ObjectUtil.isEmpty(userId)){
				userId = session.getAttribute("userId")+"";
			}
			DBTools2.update("sort,itemname,flag,xgr='"+userId+"',xgsj=now()","id", "pandian_jieguo_biaozhun_jd", parms);
			
			return new SuccessResponseData();
			
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("盘点结果标准编辑失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}

	@Override
	public ResponseData deletePandianJieguoBiaozhun(JSONObject parms) {
		try {
			DBTools2.delete("id", "pandian_jieguo_biaozhun_jd", parms);
			return new SuccessResponseData();
		} catch (SQLException throwables) {
			
			throwables.printStackTrace();
			return new ErrorResponseData("盘点结果标准删除失败");
		} catch (Exception e) {
			e.printStackTrace();
			return new ErrorResponseData(e.getMessage());
		}
	}
	
	
}

